package com.wash.shoes.controller.pc;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.common.enums.FactoryShelvesStatusEnum;
import com.wash.shoes.entity.FactoryshelvesPo;
import com.wash.shoes.entity.FactoryshelvesRackGridPo;
import com.wash.shoes.request.FactoryShelveQueryRequest;
import com.wash.shoes.request.FactoryShelvesDelRequest;
import com.wash.shoes.request.FactoryShelvesRequest;
import com.wash.shoes.request.FactoryShelvesUpdateStatusRequest;
import com.wash.shoes.service.FactoryShelvesService;
import com.wash.shoes.service.FactoryshelvesRackGridService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 工厂货架控制器
 */
@Api(tags = {"工厂货架"})
@RestController
@RequestMapping("/factoryshelves")
@RequiredArgsConstructor
public class FactoryshelvesController {

    private final FactoryShelvesService factoryShelvesService;


    private final FactoryshelvesRackGridService factoryshelvesRackGridService;

    @ApiOperation("工厂货架列表")
    @PostMapping("/list")
    public R<IPage> list(@RequestBody @Valid FactoryShelveQueryRequest factoryShelveQueryRequest) {
        IPage<FactoryshelvesPo> data = factoryShelvesService
                .page(factoryShelveQueryRequest.toPage(), new LambdaQueryWrapper<FactoryshelvesPo>()
                        .eq(Objects.nonNull(factoryShelveQueryRequest.getStatus()), FactoryshelvesPo::getStatus,
                                factoryShelveQueryRequest.getStatus())
                        .eq(Objects.nonNull(factoryShelveQueryRequest.getFactoryId()), FactoryshelvesPo::getFactoryId,
                                factoryShelveQueryRequest.getFactoryId()));

        return R.ok(data);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("工厂货架保存")
    public R<String> add(@RequestBody @Valid FactoryShelvesRequest factoryShelvesRequest) {
        FactoryshelvesPo factoryshelvesPo = factoryShelvesRequest.toPo();
        if (Objects.isNull(factoryshelvesPo.getStatus())) {
            factoryshelvesPo.setStatus(FactoryShelvesStatusEnum.ONLINE);
        }
        factoryShelvesService.saveOrUpdate(factoryshelvesPo);
        return R.ok();
    }

    @PostMapping("/updateStatus")
    @ApiOperation("工厂货架状态修改")
    public R updateStatus(@RequestBody @Valid FactoryShelvesUpdateStatusRequest factoryShelvesRequest) {
        FactoryshelvesPo factoryshelvesPo = new FactoryshelvesPo();
        factoryshelvesPo.setStatus(factoryShelvesRequest.getStatus());
        factoryshelvesPo.setId(factoryShelvesRequest.getId());
        // factoryshelvesPo.setFactoryId(factoryShelvesRequest.getFactoryId());
        factoryShelvesService.update(factoryshelvesPo,
                new LambdaQueryWrapper<FactoryshelvesPo>().eq(FactoryshelvesPo::getId, factoryShelvesRequest.getId()));

        // 更新货架下的所有格子
        factoryshelvesRackGridService.update(new LambdaUpdateWrapper<FactoryshelvesRackGridPo>()
                .eq(FactoryshelvesRackGridPo::getFactoryshelvesId, factoryShelvesRequest.getId())
                .set(FactoryshelvesRackGridPo::getStatus, factoryShelvesRequest.getStatus()));
        return R.ok();
    }

    @ApiOperation("工厂货架删除")
    @PostMapping("/delete")
    public R delFactoryShelves(@RequestBody @Valid FactoryShelvesDelRequest factoryShelvesDelRequest) {
        long count = factoryShelvesService.count(new LambdaQueryWrapper<FactoryshelvesPo>()
                .eq(FactoryshelvesPo::getFactoryId, factoryShelvesDelRequest.getFactoryId())
                .eq(FactoryshelvesPo::getId, factoryShelvesDelRequest.getId()));
        Assert.isTrue(count > 0, "工厂货架不存在");
        count = factoryshelvesRackGridService.count(new LambdaQueryWrapper<FactoryshelvesRackGridPo>().eq(FactoryshelvesRackGridPo::getFactoryshelvesId, factoryShelvesDelRequest.getId()).isNotNull(FactoryshelvesRackGridPo::getOrderId));
        Assert.isTrue(count == 0, "货架格子目前在使用,不能删除.");
        // 删除货架下的所有格子
        factoryshelvesRackGridService.update(new LambdaUpdateWrapper<FactoryshelvesRackGridPo>()
                .eq(FactoryshelvesRackGridPo::getFactoryshelvesId, factoryShelvesDelRequest.getId())
                .set(FactoryshelvesRackGridPo::getStatus, FactoryShelvesStatusEnum.OFFLINE));
        return R.ok(factoryShelvesService.removeById(factoryShelvesDelRequest.getId()));
    }

    @ApiOperation("工厂货架详情")
    @GetMapping("/getDetailById")
    public R<FactoryshelvesPo> getDetailById(String id) {
        return R.ok(factoryShelvesService.getById(id));
    }

}